<?php

require_once 'DML.class.php';
require_once 'DBCarona.class.php';
/*@author Gabriel Bronzatti Moro*/
Class DBUsuario extends DML {

    private $table;
    private $conection;
    private $instrucao;
    private $nomeUser;

    public function __construct($mysqli) {
        $this->table = "usuario";
        $this->conection = $mysqli;
    }

    public function insert($colunas, $valores) {
        if (isset($this->conection)) {
            $resultSet = $this->instrucao = $this->conection->query("INSERT INTO " . $this->table . "(" . $colunas . ") VALUES(" . $valores . ");");
            return $resultSet;
        } else
            return false;
    }

    public function select($colunas, $valores) {
        if (isset($this->conection)) {
            $resultSet = $this->instrucao = $this->conection->query("SELECT `" . $colunas[0] . "`,`" . $colunas[1] . "`,`" . $colunas[2] . "` FROM " .
                    $this->table . " WHERE `" . $colunas[1] . "`= \"" . $valores[0] . "\" and `" .
                    $colunas[2] . "`= \"" . $valores[1] . "\";");

            while ($row = $resultSet->fetch_object()) {
                $this->nomeUser = $row->nome_usuario;
                if ($row->email_usuario == $valores[0] && $row->senha_usuario == $valores[1]) {
                    return true;
                } else {
                    continue;
                }
            }
            return false;
        }
    }

    public function selectPerfil($nome, $email) {
        $vetorPerfil = null;
        if (isset($this->conection)) {
            $resultSet = $this->instrucao=$this->conection->query("SELECT `nome_usuario`, `cidade_usuario`, `estado_usuario`, `email_usuario` ".
                    "FROM `" .$this->table ."` WHERE `nome_usuario`= \"" . $nome . "\" AND `email_usuario`= \"" . $email . "\";");
            
            while ($row = $resultSet->fetch_object()) {
                $vetorPerfil = array($row->nome_usuario, $row->cidade_usuario, $row->estado_usuario, $row->email_usuario);

                return $vetorPerfil;
            }
        }
    }

    public function selectAmigo($coluna, $valor,$nomeUser,$login){
        $vetorPerfil = null;
       
        $dbCarona= new DBCarona($this->conection);
        $codigoUser= $dbCarona->recuperaCodigoUser($nomeUser, $login);
        print_r($codigoUser);
        if (isset($this->conection)) {
            $resultSet = $this->instrucao=$this->conection->query("SELECT ".$coluna.
                    "FROM `" .$this->table ."` WHERE `codigo_usuario` != \"". $codigoUser."\" and `nome_usuario` LIKE \"%" . $valor . "%\"". ";");
            
            $vetorPerfil= null;
            
            while ($row = $resultSet->fetch_object()) {
                $vetorPerfil = array($row->nome_usuario, $row->cidade_usuario,$row->email_usuario);
            }
            
            return $vetorPerfil;
        }
    }

    public function recuperaCodigoUser($nome,$login) {

        if (isset($this->conection)) {
            $resultSet = $this->instrucao = $this->conection->query("SELECT  `codigo_usuario`" . " FROM " .
                    "usuario" . " WHERE `nome_usuario`= \"" . $nome . "\" and `email_usuario`=\"".$login."\";");

            while ($row = $resultSet->fetch_object()) {
                return $row->codigo_usuario;
            }
        }
    }
    
    public function recuperaNomeUser($codigo) {

        if (isset($this->conection)) {
            $resultSet = $this->instrucao = $this->conection->query("SELECT  `nome_usuario`" . " FROM " .
                    "usuario" . " WHERE `codigo_usuario`= \"" . $codigo . "\";");

            while ($row = $resultSet->fetch_object()) {
                return $row->nome_usuario;
            }
        }
    }
    
    public function recuperaPerfil($codigo){
         if (isset($this->conection)) {
            $resultSet = $this->instrucao = $this->conection->query("SELECT  `nome_usuario`,`cidade_usuario`,`estado_usuario`,`email_usuario`" . " FROM " .
                    "usuario" . " WHERE `codigo_usuario`= \"" . $codigo . "\";");

            $arrayPerfil=null;
            $cont=0;
            while ($row = $resultSet->fetch_object()) {
                 $arrayPerfil[$cont]= $row->nome_usuario;
                 $arrayPerfil[$cont+2]= $row->cidade_usuario;
                 $arrayPerfil[$cont+3]= $row->estado_usuario;
                 $arrayPerfil[$cont+4]= $row->email_usuario;
                 break;
            }
            return $arrayPerfil;
        }
    }
    
    public function getNomeUser() {
        return $this->nomeUser;
    }
    
    public function verificarEmailExistente($email){
          if (isset($this->conection)) {
            $resultSet = $this->instrucao = $this->conection->query("SELECT email_usuario FROM " .
                    $this->table . " WHERE `email_usuario`= \"" . $email . "\";");

            while ($row = $resultSet->fetch_object()) {
                if($row->email_usuario==$email){
                    return true;
                }
            }
            return false;
        }
    }

}